package com.yuwubao.uav.tool.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * MD5通用类
 * @since 2017.04.15
 * @version 1.0.0_1
 * 
 */
public class MD5Util {

    private static final Logger logger = LogManager.getLogger(MD5Util.class);

    private static final String SECRET_KEY = "yuwubaozhiku";

    /**
     * MD5方法
     * 
     * @param text 明文
     * @return 密文
     * @throws Exception
     */
    public static String md5(String text) throws Exception {

        //加密后的字符串
        String encodeStr=DigestUtils.md5Hex(text + SECRET_KEY);

        System.out.println("MD5加密后的字符串为:encodeStr="+encodeStr);

        return encodeStr;

    }

    /**
     * MD5验证方法
     *
     * @param text 明文
     * @param md5 密文
     * @return true/false
     * @throws Exception
     */
    public static boolean verify(String text, String md5) throws Exception {

        //根据传入的密钥进行验证
        String md5Text = md5(text);
        if(md5Text.equalsIgnoreCase(md5))
        {
            logger.log(Level.INFO,"MD5验证通过");

            return true;
        }

        return false;
    }
}